package io.geekidea.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springboot.entity.BuildingType;
import io.geekidea.springboot.service.IBuildingTypeService;
import io.geekidea.springboot.vo.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/building-type")
@Tag(name = "建筑类型管理", description = "建筑类型管理接口")
public class BuildingTypeController {

    @Autowired
    private IBuildingTypeService buildingTypeService;

    @PostMapping
    @Operation(summary = "新增建筑类型")
    public ResponseResult<BuildingType> add(@Valid @RequestBody BuildingType buildingType) {
        buildingType.setId(null);
        buildingTypeService.save(buildingType);
        return ResponseResult.success(buildingType);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除建筑类型")
    public ResponseResult<Void> delete(@PathVariable Long id) {
        buildingTypeService.delete(id);
        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "更新建筑类型")
    public ResponseResult<BuildingType> update(@Valid @RequestBody BuildingType buildingType) {
        buildingTypeService.updateById(buildingType);
        return ResponseResult.success(buildingType);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询建筑类型详情")
    public ResponseResult<BuildingType> getById(@PathVariable Long id) {
        BuildingType buildingType = buildingTypeService.getById(id);
        return ResponseResult.success(buildingType);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询建筑类型列表")
    public ResponseResult<Page<BuildingType>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<BuildingType> result = buildingTypeService.page(new Page<>(page, size));
        return ResponseResult.success(result);
    }
}
